package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class DeptController {
    @Resource
    private DeptService deptService;

    @GetMapping("/depts")
    public Result list(){
        List<Dept> lists = deptService.queryAll();

        return Result.success(lists);
    }

    //根据id删除部门
    @DeleteMapping("/depts")
    public Result deleteDept(@RequestParam("id") Integer id){
        deptService.deleteById(id);
        return Result.success();
    }

    //添加部门
    @PostMapping("/depts")
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return Result.success();
    }

    //根据ID查询部门
    @GetMapping("/depts/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    //根据ID修改部门
    @PutMapping("/depts")
    public Result updateById(@RequestBody Dept dept){
        deptService.updateById(dept);
        return Result.success();
    }
}
